﻿using System.Linq;
using ff14bot;
using ff14bot.Enums;
using ff14bot.Managers;
using ff14bot.Objects;

namespace Magitek.Helpers
{
    public class Target
    {
        BattleCharacter target;
        public void GetTarget()
        {
            target = null;

            if (Magitek.WindowSettings.SummonChocobo && !Chocobo.Summoned && Chocobo.CanSummon)
            {
                Chocobo.Summon();
            }

            if (Magitek.WindowSettings.TargetAssistTank)
            {
                if (PartyManager.IsInParty)
                {
                    var tank =
                        (from unit in PartyManager.VisibleMembers
                         where unit.Class.ToString() == "Warrior" || unit.Class.ToString() == "Paladin" || unit.Class.ToString() == "Gladiator" || unit.Class.ToString() == "Marauder"
                         orderby unit.MaxHealth descending
                         select unit).FirstOrDefault();

                    if (tank != null && (tank.GameObject as BattleCharacter).HasTarget)
                    {
                        target =
                            (from unit in GameObjectManager.GetObjectsOfType<BattleCharacter>()
                             where unit.Location.Distance(Core.Player.Location) < Magitek.WindowSettings.TargetDistance
                             where unit.ObjectId == (tank.GameObject as BattleCharacter).CurrentTargetId
                             select unit).FirstOrDefault();


                        if (Core.Player.HasTarget)
                        {
                            if (Core.Player.CurrentTarget.ObjectId != (tank.GameObject as BattleCharacter).CurrentTargetId)
                            {
                                if (target != null)
                                {
                                    target.Target();
                                }
                            }
                        }

                        if (!Core.Player.HasTarget)
                        {
                            if (target != null)
                            {
                                target.Target();
                            }
                        }
                    }

                }
            }

            if (Magitek.WindowSettings.TargetAssistDPS)
            {
                if (PartyManager.IsInParty)
                {
                    var selection =
                        (from unit in PartyManager.VisibleMembers
                         where unit.Class.ToString() == "Summoner" || unit.Class.ToString() == "Bard" || unit.Class.ToString() == "Monk" || unit.Class.ToString() == "Dragoon" || unit.Class.ToString() == "BlackMage"
                         where unit.ObjectId != Core.Player.ObjectId
                         orderby unit.MaxHealth descending
                         select unit).FirstOrDefault();

                    if (selection != null && (selection.GameObject as BattleCharacter).HasTarget)
                    {
                        target =
                            (from unit in GameObjectManager.GetObjectsOfType<BattleCharacter>()
                             where unit.Location.Distance(Core.Player.Location) < Magitek.WindowSettings.TargetDistance
                             where unit.ObjectId == (selection.GameObject as BattleCharacter).CurrentTargetId
                             select unit).FirstOrDefault();


                        if (Core.Player.HasTarget)
                        {
                            if (Core.Player.CurrentTarget.ObjectId != (selection.GameObject as BattleCharacter).CurrentTargetId)
                            {
                                if (target != null)
                                {
                                    target.Target();
                                }
                            }
                        }

                        if (!Core.Player.HasTarget)
                        {
                            if (target != null)
                            {
                                target.Target();
                            }
                        }
                    }

                }
            }


            if (Magitek.WindowSettings.TargetFateEnemies)
            {
                target =
                    (from unit in GameObjectManager.GetObjectsOfType<BattleCharacter>()
                    where unit.Type == GameObjectType.BattleNpc
                    where unit.Location.Distance(Core.Player.Location) < Magitek.WindowSettings.TargetDistance
                    where unit.CanAttack
                    where unit.IsVisible
                    where unit.IsFate
                    select unit).FirstOrDefault();

                if (target != null && !Core.Player.HasTarget)
                {
                    target.Target();
                }
            }

            if (Magitek.WindowSettings.TargetClosestEnemy)
            {
                target =
                    (from unit in GameObjectManager.GetObjectsOfType<BattleCharacter>()
                     where unit.Type == GameObjectType.BattleNpc
                     where unit.Location.Distance(Core.Player.Location) < Magitek.WindowSettings.TargetDistance
                     where unit.CanAttack
                     where unit.IsVisible
                     orderby unit.Distance(Core.Player.Location) ascending 
                     select unit).FirstOrDefault();

                if (target != null && (!Core.Player.HasTarget || !Core.Player.CurrentTarget.CanAttack))
                {
                    target.Target();
                }
            }

            if (Magitek.WindowSettings.TargetLowestHealth)
            {
                target =
                    (from unit in GameObjectManager.GetObjectsOfType<BattleCharacter>()
                     where unit.Location.Distance(Core.Player.Location) < Magitek.WindowSettings.TargetDistance
                     where unit.CanAttack
                     where unit.IsVisible
                     orderby unit.CurrentHealthPercent ascending 
                     select unit).FirstOrDefault();

                if (target != null && (!Core.Player.HasTarget || !Core.Player.CurrentTarget.CanAttack))
                {
                    target.Target();
                }
            }

            if (Core.Player.HasTarget && !Core.Player.InCombat && Core.Player.CurrentTarget.IsVisible &&
                Core.Player.CurrentTarget.Location.Distance(Core.Player.Location) <=
                Magitek.WindowSettings.TargetDistance && Magitek.WindowSettings.TargetStartCombat)
            {
                if (Actionmanager.InSpellInRangeLOS(BestPullSpell, target) == SpellRangeCheck.Success && !Core.Player.IsCasting && Actionmanager.CanCast(BestPullSpell, target))
                {
                        Actionmanager.DoAction(BestPullSpell, target);
                        //Extensions.UpdateDoubleCastDict(BestPullSpell, target);
                }
            }

        }
        private string[] PullSpells = new[] { "Tomahawk", "Fast Blade", "Shield Lob", "Straight Shot", "Heavy Swing", "Bootshine", "True Thrust", "Blizzard", "Ruin", "Stone", "Heavy Shot" };
        private string _BestPullSpell;
        private string BestPullSpell
        {
            get
            {
                if (string.IsNullOrEmpty(_BestPullSpell))
                {
                    foreach (var spell in PullSpells)
                    {
                        if (Actionmanager.HasSpell(spell))
                        {
                            _BestPullSpell = spell;
                            return spell;
                        }
                    }

                    return null;

                }
                else
                {
                    return _BestPullSpell;
                }
            }
        }
    }
}
